package com.sykj.exception;

import com.sykj.utils.BusinessException;
import com.sykj.utils.ResponseResult;
import com.sykj.utils.ResultCode;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionControllerAdvice {


    @ExceptionHandler(BindException.class)
    public ResponseResult BindExceptionHandler(BindException e) {
        // 然后提取错误提示信息进行返回
        return new ResponseResult(ResultCode.VALIDATE_FAILED,e.getAllErrors().get(0).getDefaultMessage());
    }


    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        // 然后提取错误提示信息进行返回
        return new ResponseResult(ResultCode.VALIDATE_FAILED,e.getBindingResult().getAllErrors().get(0));
    }
    @ExceptionHandler(BusinessException.class)
    public ResponseResult BusinessException(BusinessException e) {
        return new ResponseResult(e.getCode(),e.getMessage(),null);
    }

}